﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;

namespace Songhay.Data.GenericWeb.InputDefinitions
{
    using Songhay.Data.GenericWeb.DataTransferContracts;
    using Songhay.Xml;

    /// <summary>
    /// Defines input for the Segment-Document data transfer contract.
    /// </summary>
    public class InputSegmentDocument : ISegmentDocument
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InputSegmentDocument"/> class.
        /// </summary>
        /// <param name="documentId">The Document ID.</param>
        /// <param name="segmentId">The Segment ID.</param>
        public InputSegmentDocument(int? documentId, int? segmentId)
        {
            this._documentId = documentId;
            this._segmentId = segmentId;
        }

        /// <summary>
        /// Gets the list of Segment-Document from the input.
        /// </summary>
        /// <param name="navigableInput">The navigable input.</param>
        /// <returns>Returns the list of Segment-Document data transfer contract objects.</returns>
        public static List<ISegmentDocument> GetList(IXPathNavigable navigableInput)
        {
            bool throwExceptionForMissingDocumentIds = false;
            return GetList(navigableInput, throwExceptionForMissingDocumentIds);
        }

        /// <summary>
        /// Gets the list of Segment-Document from the input.
        /// </summary>
        /// <param name="navigableInput">The navigable input.</param>
        /// <param name="throwExceptionForMissingDocumentIds">When <c>true</c> throw an exception for missing Document IDs.</param>
        /// <returns>Returns the list of Segment-Document data transfer contract objects.</returns>
        public static List<ISegmentDocument> GetList(IXPathNavigable navigableInput, bool throwExceptionForMissingDocumentIds)
        {
            List<ISegmentDocument> list = new List<ISegmentDocument>();
            XPathNavigator nodeDocumentSegments = XmlUtility.GetNavigableNode(navigableInput, "//DocumentSegments");

            if(nodeDocumentSegments != null)
            {
                XPathNavigator segmentDocument = null; object documentId = null; object segmentId = null;
                foreach(IXPathNavigable n in nodeDocumentSegments.SelectChildren(XPathNodeType.Element))
                {
                    segmentDocument = n.CreateNavigator();
                    documentId = XmlUtility.GetNodeValue(segmentDocument, "./DocumentId", throwExceptionForMissingDocumentIds);
                    segmentId = XmlUtility.GetNodeValue(segmentDocument, "./SegmentId", true);
                    list.Add(new InputSegmentDocument(FrameworkType.ParseInt32(documentId), FrameworkType.ParseInt32(segmentId)));
                }
            }

            return list;
        }

        #region IDocumentSegment Members

        /// <summary>
        /// Gets or sets the Document ID.
        /// </summary>
        /// <value>The Document ID.</value>
        public int? DocumentId
        {
            get
            {
                return _documentId;
            }
            set
            {
                this._documentId = value;
            }
        }


        /// <summary>
        /// Gets or sets the Segment ID.
        /// </summary>
        /// <value>The Segment ID.</value>
        public int? SegmentId
        {
            get
            {
                return _segmentId;
            }
            set
            {
                this._segmentId = value;
            }
        }

        #endregion

        private int? _documentId;
        private int? _segmentId;
    }
}
